package com.campus.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campus.entity.Clazz.ClazzStudent;
import com.campus.entity.NoteForLeave;
import com.campus.entity.ResultBody;
import com.campus.entity.Student;
import com.campus.entity.bo.StudentAttendanceInfoBO;
import com.campus.entity.vo.AttendanceVO;
import com.campus.mapper.ClazzStudentMapper;
import com.campus.mapper.LeaveMapper;
import com.campus.mapper.StudentMapper;
import com.campus.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class AttendController {

    @Autowired
    private ClazzStudentMapper clazzStudentMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private ExcelUtil excelUtil;

    //出勤表
    @RequestMapping("/attendance/{classid}")
    public ResultBody attendance(@PathVariable("classid") Integer classid,@RequestBody Map<String,Integer> map){
        Set<Long> students = clazzStudentMapper.selectList(new QueryWrapper<ClazzStudent>().eq("class_id", classid)).stream().map(ClazzStudent::getStudentId).collect(Collectors.toSet());
        Map<String, AttendanceVO> data = new HashMap<>();
        for(Long s:students){
            Student student = studentMapper.selectById(s);
            NoteForLeave leave = leaveMapper.selectOne(new QueryWrapper<NoteForLeave>().eq("account", student.getAccount()).eq("week", map.get("week")).eq("day", map.get("day")).eq("attend", map.get("attend")));
            if(leave==null){
                data.put(student.getAccount(),new AttendanceVO("出勤",student));
            }else{
                data.put(student.getAccount(),new AttendanceVO(leave.getType(),student));
            }
        }
        return ResultBody.ok().data("data",data);
    }


    @RequestMapping("/attendance/download/{classid}")
    public ResultBody download(@PathVariable("classid") Integer classid,@RequestBody Map<String,Integer> map) throws IOException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        List<Long> students = clazzStudentMapper.selectList(new QueryWrapper<ClazzStudent>().eq("class_id", classid)).stream().map(ClazzStudent::getStudentId).collect(Collectors.toList());
        List<Student> studentsInfo = studentMapper.selectList(new QueryWrapper<Student>().in("id", students));
        List<StudentAttendanceInfoBO> data = new ArrayList<>();
        for(Student s:studentsInfo){
            StudentAttendanceInfoBO infoBO = new StudentAttendanceInfoBO();
            BeanUtil.copyProperties(s,infoBO);
            NoteForLeave account = leaveMapper.selectOne(new QueryWrapper<NoteForLeave>().eq("account", s.getAccount()).eq("week",map.get("week")).eq("day",map.get("day")));
            if(account==null){
                infoBO.setStatus("出勤");
            }else{
                infoBO.setStatus(account.getType());
            }
            data.add(infoBO);
        }
        String excelPath = excelUtil.generateExcel(data, "班级打卡出勤表");
        return ResultBody.ok().data("path",excelPath);
    }


    //请假，需要传入班级id，学生account，第几周，第几天，第几节课
    @RequestMapping("/askforleave")
    public ResultBody forLeave(@RequestBody NoteForLeave leave){
        System.out.println(leave);
        leaveMapper.insert(leave);
        return ResultBody.ok();
    }
}
